package com.slwish.xty.ui.activity;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.slwish.common.base.PresenterNoToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.utils.IntentUtils;
import com.slwish.common.utils.LogUtils;
import com.slwish.common.utils.SpUtils;
import com.slwish.common.utils.StatusBarUtils;
import com.slwish.common.utils.StringUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.GlideImageLoader;
import com.slwish.xty.entity.CreditTourismInfoModel;
import com.slwish.xty.entity.Doclass;
import com.slwish.xty.mvp.contract.CreditTourismInfoContract;
import com.slwish.xty.mvp.presenter.CreditTourismlnfoPresenter;
import com.slwish.xty.utils.LocalUtils;
import com.slwish.xty.widget.AbilityMapView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商店详情
 */
public class CreditTourismInfoActivity extends PresenterNoToolbarActivity<CreditTourismInfoContract.Presenter> implements CreditTourismInfoContract.View {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_titles)
    TextView tvTitle;
    @BindView(R.id.ability_mapview)
    AbilityMapView abilityMapview;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar)
    AppBarLayout appBarLayout;
    @BindView(R.id.toolbar_layout)
    CollapsingToolbarLayout toolbarLayout;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    private int merId;
    CreditTourismInfoModel model;

    @Override
    protected int getLayoutId() {
        return R.layout.act_credit_tourism_info2;
    }

    @Override
    protected boolean isShowStatusBar() {
        return false;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        StatusBarUtils.setTransparentForWindow(this);

        setTitle(R.string.credit_tourism_title);
        merId = getIntent().getIntExtra(Contants.ID, 0);
        model = (CreditTourismInfoModel) getIntent().getSerializableExtra(Contants.MODEL);
        //设置Banner样式
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR).setIndicatorGravity(BannerConfig.RIGHT);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        if (model == null)
            mPresenter.queryMerchant(mContext, merId);
        else setData();

    }

    private void setToolBarLayout(String newsTitle) {
        toolbar.setTitle(newsTitle);
        toolbarLayout.setExpandedTitleColor(ContextCompat.getColor(this, R.color.white));
        toolbarLayout.setCollapsedTitleTextColor(ContextCompat.getColor(this, R.color.white));
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset == 0) {
                    //全展开
                    if (state != CollapsingToolbarLayoutState.EXPANDED) {
                        state = CollapsingToolbarLayoutState.EXPANDED;//修改状态标记为展开
                        //Ctl_toolbar.setTitle("心灵鸡汤");//设置title为EXPANDED
                    }
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    if (state != CollapsingToolbarLayoutState.COLLAPSED) {

                        tvTitle.setVisibility(View.VISIBLE);
                        state = CollapsingToolbarLayoutState.COLLAPSED;//修改状态标记为折叠

                    }
                } else {
                    if (state != CollapsingToolbarLayoutState.INTERNEDIATE) {
                        if (state == CollapsingToolbarLayoutState.COLLAPSED) {
                            tvTitle.setVisibility(View.GONE);//由折叠变为中间状态时隐藏播放按钮
                        }
                        state = CollapsingToolbarLayoutState.INTERNEDIATE;//修改状态标记为中间
                    }
                }
            }
        });
    }

    /**
     * 绑定数据
     */
    private void setData() {
        try {
            tvName.setText(model.getScenicName());
            tvLocation.setText(model.getAddress());
            tvLevel.setText(model.getCreditGrade());
            banner.setImages(model.getLogo()).start();
            List<Doclass> doClass = model.getDoClass();
            List<String> list = new ArrayList<>();
            List<Integer> intList = new ArrayList<>();
            for (Doclass model : doClass) {
                list.add(model.getDoPoint());
                if (model.getDoPoint() != null &&!model.getDoPoint().equals(""))
                    intList.add(Integer.valueOf(model.getDoPoint()));
                else intList.add(0);
            }
            abilityMapview.setData(intList);
        } catch (Exception e) {
            LogUtils.i("xx",e.getMessage());
        }

    }

    @Override
    protected CreditTourismInfoContract.Presenter initPresenter() {
        return new CreditTourismlnfoPresenter(this);
    }

    @OnClick({R.id.tv_msg, R.id.iv_call, R.id.tv_location
            , R.id.tv_cxcd, R.id.tv_xygs, R.id.tv_yhpj, R.id.tv_yy, R.id.iv_left})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_call://拨号
                if (model == null || StringUtils.isEmpty(model.getMobile()) || model.getMobile() == null)
                    showError(R.string.toast_no_lxfs_text);
                else
                    IntentUtils.callPhone(mContext, model.getMobile());
                break;
            case R.id.tv_location://位置
                LocalUtils.intentToMap(mContext, tvLocation.getText().toString(), model.getLongitude(), model.getLatitude());
                break;
            case R.id.tv_msg://商家信息
                Bundle bundle = new Bundle();
                bundle.putSerializable(Contants.MODEL, model);
                IntentUtils.startActivity(mContext, BusinessDetailsActivity.class, bundle);
                break;
            case R.id.tv_cxcd://诚信菜单
                IntentUtils.startActivity(mContext, SincerityMenuActivity.class);
                break;
            case R.id.tv_xygs://信用公示
                IntentUtils.startActivity(mContext, CreditPublicityInfoActivity.class);
                break;
            case R.id.tv_yhpj://用户评价
                Bundle bundles = new Bundle();
                bundles.putInt(Contants.ID, merId);
                IntentUtils.startActivity(mContext, UserCommentListActivity.class, bundles);
                break;
            case R.id.tv_yy://提交
                long userId = SpUtils.getLong(mContext, Contants.UserCode, 0);
                if (userId > 0) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putInt(Contants.ID, merId);
                    IntentUtils.startActivity(mContext, SubscribeEditActivity.class, bundle1);
                } else {
                    IntentUtils.startActivity(mContext, LoginActivity.class);
                }
                break;
            case R.id.iv_left://返回
                finish();
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            //结束轮播
            banner.stopAutoPlay();
        } catch (Exception e) {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            //结束轮播
            banner.stopAutoPlay();
        } catch (Exception e) {
        }

    }

    @Override
    public void queryMerchantSuccess(CreditTourismInfoModel model) {
        this.model = model;
        try {
            setData();
//            tvName.setText(model.getScenicName());
//            tvLocation.setText(model.getAddress());
//            tvLevel.setText(model.getCreditGrade());
//            banner.setImages(model.getLogo()).start();


        } catch (Exception e) {
        }
    }

    private CollapsingToolbarLayoutState state = CollapsingToolbarLayoutState.EXPANDED;

    private enum CollapsingToolbarLayoutState {
        EXPANDED,
        COLLAPSED,
        INTERNEDIATE
    }

}
